<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="类型"></el-input>
    </el-form-item>
    <el-form-item label="上报时间" prop="time">
      <el-input v-model="dataForm.time" placeholder="上报时间"></el-input>
    </el-form-item>
    <el-form-item label="采集时间" prop="datatime">
      <el-input v-model="dataForm.datatime" placeholder="采集时间"></el-input>
    </el-form-item>
    <el-form-item label="网关号" prop="gwsn">
      <el-input v-model="dataForm.gwsn" placeholder="网关号"></el-input>
    </el-form-item>
    <el-form-item label="仪表号" prop="metersn">
      <el-input v-model="dataForm.metersn" placeholder="仪表号"></el-input>
    </el-form-item>
    <el-form-item label="仪表名" prop="metername">
      <el-input v-model="dataForm.metername" placeholder="仪表名"></el-input>
    </el-form-item>
    <el-form-item label="仪表状态" prop="meterstatus">
      <el-input v-model="dataForm.meterstatus" placeholder="仪表状态"></el-input>
    </el-form-item>
    <el-form-item label="" prop="ch">
      <el-input v-model="dataForm.ch" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="电压" prop="u">
      <el-input v-model="dataForm.u" placeholder="电压"></el-input>
    </el-form-item>
    <el-form-item label="电流" prop="i">
      <el-input v-model="dataForm.i" placeholder="电流"></el-input>
    </el-form-item>
    <el-form-item label="功率" prop="p">
      <el-input v-model="dataForm.p" placeholder="功率"></el-input>
    </el-form-item>
    <el-form-item label="温度" prop="temp">
      <el-input v-model="dataForm.temp" placeholder="温度"></el-input>
    </el-form-item>
    <el-form-item label="正向有功电能" prop="epi">
      <el-input v-model="dataForm.epi" placeholder="正向有功电能"></el-input>
    </el-form-item>
    <el-form-item label="反向有功电能" prop="epe">
      <el-input v-model="dataForm.epe" placeholder="反向有功电能"></el-input>
    </el-form-item>
    <el-form-item label="电流变比" prop="pt">
      <el-input v-model="dataForm.pt" placeholder="电流变比"></el-input>
    </el-form-item>
    <el-form-item label="电压变比" prop="ct">
      <el-input v-model="dataForm.ct" placeholder="电压变比"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          type: '',
          time: '',
          datatime: '',
          gwsn: '',
          metersn: '',
          metername: '',
          meterstatus: '',
          ch: '',
          u: '',
          i: '',
          p: '',
          temp: '',
          epi: '',
          epe: '',
          pt: '',
          ct: ''
        },
        dataRule: {
          type: [
            { required: true, message: '类型不能为空', trigger: 'blur' }
          ],
          time: [
            { required: true, message: '上报时间不能为空', trigger: 'blur' }
          ],
          datatime: [
            { required: true, message: '采集时间不能为空', trigger: 'blur' }
          ],
          gwsn: [
            { required: true, message: '网关号不能为空', trigger: 'blur' }
          ],
          metersn: [
            { required: true, message: '仪表号不能为空', trigger: 'blur' }
          ],
          metername: [
            { required: true, message: '仪表名不能为空', trigger: 'blur' }
          ],
          meterstatus: [
            { required: true, message: '仪表状态不能为空', trigger: 'blur' }
          ],
          ch: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          u: [
            { required: true, message: '电压不能为空', trigger: 'blur' }
          ],
          i: [
            { required: true, message: '电流不能为空', trigger: 'blur' }
          ],
          p: [
            { required: true, message: '功率不能为空', trigger: 'blur' }
          ],
          temp: [
            { required: true, message: '温度不能为空', trigger: 'blur' }
          ],
          epi: [
            { required: true, message: '正向有功电能不能为空', trigger: 'blur' }
          ],
          epe: [
            { required: true, message: '反向有功电能不能为空', trigger: 'blur' }
          ],
          pt: [
            { required: true, message: '电流变比不能为空', trigger: 'blur' }
          ],
          ct: [
            { required: true, message: '电压变比不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/pointhisdata/pointdatatest/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.type = data.pointDataTest.type
                this.dataForm.time = data.pointDataTest.time
                this.dataForm.datatime = data.pointDataTest.datatime
                this.dataForm.gwsn = data.pointDataTest.gwsn
                this.dataForm.metersn = data.pointDataTest.metersn
                this.dataForm.metername = data.pointDataTest.metername
                this.dataForm.meterstatus = data.pointDataTest.meterstatus
                this.dataForm.ch = data.pointDataTest.ch
                this.dataForm.u = data.pointDataTest.u
                this.dataForm.i = data.pointDataTest.i
                this.dataForm.p = data.pointDataTest.p
                this.dataForm.temp = data.pointDataTest.temp
                this.dataForm.epi = data.pointDataTest.epi
                this.dataForm.epe = data.pointDataTest.epe
                this.dataForm.pt = data.pointDataTest.pt
                this.dataForm.ct = data.pointDataTest.ct
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/pointhisdata/pointdatatest/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'type': this.dataForm.type,
                'time': this.dataForm.time,
                'datatime': this.dataForm.datatime,
                'gwsn': this.dataForm.gwsn,
                'metersn': this.dataForm.metersn,
                'metername': this.dataForm.metername,
                'meterstatus': this.dataForm.meterstatus,
                'ch': this.dataForm.ch,
                'u': this.dataForm.u,
                'i': this.dataForm.i,
                'p': this.dataForm.p,
                'temp': this.dataForm.temp,
                'epi': this.dataForm.epi,
                'epe': this.dataForm.epe,
                'pt': this.dataForm.pt,
                'ct': this.dataForm.ct
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
